#!/usr/bin/env groovy
package com.yidao.www

def autoBuild(project, branch){

    def qtProfile = qtProfile(project, branch)
    println qtProfile
    def date = new Date()

    def label = qtProfile.label + '_' + BUILD_NUMBER
    def image = qtProfile.image
    def gitUrl = qtProfile.gitUrl
    def scriptDir = qtProfile.scriptDir
    def buildScript = qtProfile.buildScript
    def packageScript = qtProfile.packageScript
    def artifactDir = qtProfile.artifactDir
    def artifactName = qtProfile.artifactName
    def artifact = qtProfile.artifact
    def artifactory = qtProfile.artifactory

    def base = new base()
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url

    def buildStatus
    def packageStatus
    def storageURL

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins') {

            node(label){

                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    if (project == 'remoteControl'){
                    base.gitlabCheckout(gitUrl, branch, '')
                    }else base.gitClone(gitUrl, branch)
                }

                stage('build'){
                    if(scriptDir){
                        dir(scriptDir){
                            buildStatus = base.scriptStatus(buildScript)
                        }
                    }else{
                        buildStatus = base.scriptStatus(buildScript)
                    }
                    if(buildStatus != 0){
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error '[ERROR] 编译失败!'
                    }
                }

                stage('package'){
                    if(packageScript){
                        packageStatus = base.scriptStatus(packageScript)
                    }else{
                        println '无需打包'
                        packageStatus = 0
                    }
                    if(packageStatus != 0){
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error '[ERROR] 编译失败!'
                    }
                }

                stage('upload'){
                    try{
                        println 'artifact: ' + artifact
                        println 'deployTarget: ' + deployTarget
                        sh "ls ${artifact}"
                        base.fileUpload(artifact, deployTarget)
                    }catch(e){
                        println "[ERROR] 上传失败!"
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('return artifact'){
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }
        }

}

def preAutoBuild(project, branch){

    def qtProfile = qtProfile(project, branch)
    println qtProfile
    def date = new Date()

    def label = qtProfile.label + '_' + BUILD_NUMBER
    def image = qtProfile.image
    def gitUrl = qtProfile.gitUrl
    def scriptDir = qtProfile.scriptDir
    def buildScript = qtProfile.buildScript
    def packageScript = qtProfile.packageScript
    def cleanScript = qtProfile.cleanScript
    def artifactDir = qtProfile.artifactDir
    def artifactName = qtProfile.artifactName
    def artifact = qtProfile.artifact
    def artifactory = qtProfile.artifactory
    def cherryPickDir = qtProfile.cherryPickDir
    def cherryPickGit = qtProfile.cherryPickGit

    def base = new base()
    def notification =new notification()
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url

    def buildStatus
    def packageStatus
    def storageURL

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins') {

            node(label){

                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    base.gitClone(gitUrl, branch)
                }

                stage('cherry-pick'){
                    if(cleanScript){
                        def status = base.scriptStatus(cleanScript)
                    }
                    dir(cherryPickDir){
                        try{
                            def status = base.cherryPick(cherryPickGit, GERRIT_REFSPEC)
                            if(status != 0){
                                currentBuild.result = 'FAILURE'
                                base.influxDb()
                                error '[ERROR] cherry-pick 失败!请检查是否有冲突或者已 Merge'
                            }
                        }catch(e){
                            println '[ERROR] cherry-pick 失败!'
                            println e
                            base.cherryPickFailClean()
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            // notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, GERRIT_PATCHSET_UPLOADER_NAME, currentBuild.currentResult)
                            error e.getMessage()
                        }
                    }
                }

                stage('build'){
                    if(scriptDir){
                        dir(scriptDir){
                            buildStatus = base.scriptStatus(buildScript)
                        }
                    }else{
                        buildStatus = base.scriptStatus(buildScript)
                    }
                    if(buildStatus != 0){
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error '[ERROR] 编译失败!'
                    }
                }

                stage('package'){
                    if(packageScript){
                        packageStatus = base.scriptStatus(packageScript)
                    }else{
                        println '无需打包'
                        packageStatus = 0
                    }
                    if(packageStatus != 0){
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error '[ERROR] 编译失败!'
                    }
                }

                stage('upload'){
                    try{
                        println 'artifact: ' + artifact
                        println 'deployTarget: ' + deployTarget
                        sh "ls ${artifact}"
                        base.fileUpload(artifact, deployTarget)
                    }catch(e){
                        println "[ERROR] 上传失败!"
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('return artifact'){
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }
        }
}

def autoBuildWin(project, branch){

    def qtProfile = qtProfile(project, branch)
    println qtProfile
    def date = new Date()

    def label = qtProfile.label
    def gitUrl = qtProfile.gitUrl
    def scriptDir = qtProfile.scriptDir
    def buildScript = qtProfile.buildScript
    def packageScript = qtProfile.packageScript
    def artifactDir = qtProfile.artifactDir
    def artifactName = qtProfile.artifactName
    def artifact = qtProfile.artifact
    def artifactory = qtProfile.artifactory

    def base = new base()
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url

    def buildStatus
    def packageStatus
    def storageURL

    node(label){
        stage('checkout'){
            storageURL = srverUrl + '/' + deployTarget
            base.gitClone(gitUrl, branch)
        }

        stage('build'){
            if(scriptDir){
                dir(scriptDir){
                    buildStatus = base.batScriptStatus(buildScript)
                }
            }else{
                buildStatus = base.batScriptStatus(buildScript)
            }
            if(buildStatus != 0){
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error '[ERROR] 编译失败!'
            }
        }

        stage('package'){
            if(packageScript){
                packageStatus = base.batScriptStatus(packageScript)
            }else{
                println '无需打包'
                packageStatus = 0
            }
            if(packageStatus != 0){
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error '[ERROR] 编译失败!'
            }
        }

        stage('upload'){
            try{
                println 'artifact: ' + artifact
                println 'deployTarget: ' + deployTarget
                base.fileUpload(artifact, deployTarget)
            }catch(e){
                println "[ERROR] 上传失败!"
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }

        stage('influxDb'){
            currentBuild.result = 'SUCCESS'
            base.influxDb()
        }

        stage('clean'){
            cleanWs()
        }

        stage('return artifact'){
            currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
            return deployTarget
        }

    }

}

def preAutoBuildWin(project, branch){

    def qtProfile = qtProfile(project, branch)
    println qtProfile
    def date = new Date()

    def label = qtProfile.label
    def gitUrl = qtProfile.gitUrl
    def scriptDir = qtProfile.scriptDir
    def buildScript = qtProfile.buildScript
    def packageScript = qtProfile.packageScript
    def cleanScript = qtProfile.cleanScript
    def artifactDir = qtProfile.artifactDir
    def artifactName = qtProfile.artifactName
    def artifact = qtProfile.artifact
    def artifactory = qtProfile.artifactory
    def cherryPickDir = qtProfile.cherryPickDir
    def cherryPickGit = qtProfile.cherryPickGit

    def base = new base()
    def notification =new notification()
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url

    def buildStatus
    def packageStatus
    def storageURL

    node(label){

        stage('checkout'){
                storageURL = srverUrl + '/' + deployTarget
                base.gitClone(gitUrl, branch)
        }
        stage('cherry-pick'){
                if(cleanScript){
                    def status = base.batscriptStatus(cleanScript)
                }
                dir(cherryPickDir){
                    try{
                        def status = base.batCherryPick(cherryPickGit, GERRIT_REFSPEC)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] cherry-pick 失败!请检查是否有冲突或者已 Merge'
                        }
                    }catch(e){
                        println '[ERROR] cherry-pick 失败!'
                        println e
                        base.batCherryPickFailClean()
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        // notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, GERRIT_PATCHSET_UPLOADER_NAME, currentBuild.currentResult)
                        error e.getMessage()
                    }
                }
        }
        stage('build'){
                if(scriptDir){
                    dir(scriptDir){
                        buildStatus = base.batscriptStatus(buildScript)
                    }
                }else{
                    buildStatus = base.batscriptStatus(buildScript)
                }
                if(buildStatus != 0){
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error '[ERROR] 编译失败!'
                }
        }
        stage('package'){
                if(packageScript){
                    packageStatus = base.batscriptStatus(packageScript)
                }else{
                    println '无需打包'
                    packageStatus = 0
                }
                if(packageStatus != 0){
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error '[ERROR] 编译失败!'
                }
        }
        stage('upload'){
                try{
                    println 'artifact: ' + artifact
                    println 'deployTarget: ' + deployTarget
                    base.fileUpload(artifact, deployTarget)
                }catch(e){
                    println "[ERROR] 上传失败!"
                    println e
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error e.getMessage()
                }
        }
        stage('influxDb'){
                currentBuild.result = 'SUCCESS'
                base.influxDb()
        }
        stage('return artifact'){
                currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                return deployTarget
        }

    }

}